<?php
##############################################
#
#   评论管理
# @ Time : 2017-1-8 - 下午9:20:27
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################


namespace Admin\Model;

class CommentModel extends AdminBaseModel{
	/*评论表  */
	protected $tableName = 'comment';	
	
	public function __construct($tableName=''){
		//初始化数据库对象
		if(!empty($tableName)){
			$this -> tableName = $tableName;
		}	
		parent::__construct($this -> tableName);
	}
	
	/**
	 * 获取评论
	 */
	public function getComment($activity_name, $type, $category){
	 	if($activity_name){
			$map['activity_name'] = array('like','%'.$activity_name.'%');
		}
		if($type){
			$map['type'] = $type;
		} 
		if($category){
		    $map['category'] = $category;
		}
	    $count = $this -> where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 1);
	    $show = $Page->show();
	    $result = $this->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		return array($result, $show);
	}
	
	/**
	 * 获取一个活动
	 */
	public function getActivityFind($id){
	    return $this->where("id = $id")->find();
	}
	
	/**
	 * 修改活动
	 */
	public function editActivity($id, $data){
	    return $this->where("id = $id")->save($data);
	    
	}
	
	/**
	* 删除评论
	*/
	public function deleteComment($id){
		return $this->where("id = $id")->delete();
	}
    
	/**
	 * 全选删除活动
	 */
	public function deleteAllActivity($idArr) {
	    foreach ($idArr['activity_id'] as $k => $v){
	        $this->where("id = $v")->delete();
	    }
	    return true;
	}
	
	/**
	 * 操作
	 */
	public function operate($idArr, $operate){
        switch ($operate) {
            case 'show':
                
            break;
            
            case 'hiden':
                
            break;
            
            case 'del':
                
            break;
        }
	}
	
	public function operateFor($v, $act){
	    foreach ($idArr['comment_id'] as $k => $v){
	        $this->where("id = $v")->delete();
	    }
	}
	
	/**
	 * 获取分类列表
	 */
	public function getCategory($name){
	    if($name){
	        $map['name'] = array('like','%'.$name.'%');
	    }
	    $count = $this -> where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 10);
	    $show = $Page->show();
	    $model = M('activity_category');
	    $result = $model->where($map)->order('sort asc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    return array($result, $show);
	}
	
	/**
	 * 获取分类字段
	 */
	public function getCategoryField(){
	    $model = M('activity_category');
	    return $model->field('id, name')->order('sort asc')->select();
	}
	
	/**
	 * 获取分类名称
	 */
	public function getCategoryName($id){
	    $model = M('activity_category');
	    return $model->where("id = $id")->getField('name');
	}
	
	/**
	 * 获取一条分类
	 */
	public function getCategoryFind($id) {
	    $model = M('activity_category');
	    return $model->where("id = $id")->find();
	}
	
	/**
	 * 修改分类
	 */
	public function editCategory($id, $data) {
	    $admin_base_model = new AdminBaseModel('activity_category');
	    return $admin_base_model->editData(array('id'=>$id), $data);
	}
	
	/**
	 * 删除分类
	 */
	public function deleteCategory($id) {
	    $admin_base_model = new AdminBaseModel('activity_category');
	    return $admin_base_model->deleteData(array('id'=>$id));
	}
	
	/**
	 * 全选删除分类
	 */
	public function deleteAllCategory($idArr) {
	    $model = M('activity_category');
	    foreach ($idArr['category_id'] as $k => $v){
	        $model->where("id = $v")->delete();
	    }
	    return true;
	}
	
}